UpdateEventCommandHandler   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 30
dl 0
loc 35
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A execute 0 24 2
1
import { CommandHandler } from '@nestjs/cqrs';
2
import { Inject } from '@nestjs/common';
3
import { UpdateEventCommand } from './UpdateEventCommand';
4
import { IEventRepository } from 'src/Domain/Calendar/Repository/IEventRepository';
5
import { ISchoolRepository } from 'src/Domain/School/Repository/ISchoolRepository';
6
import { IUserRepository } from 'src/Domain/User/Repository/IUserRepository';
7
import { AbstractEventCommandHandler } from './AbstractEventCommandHandler';
8
import { EventNotFoundException } from 'src/Domain/Calendar/Exception/EventNotFoundException';
9
10
@CommandHandler(UpdateEventCommand)
11
export class UpdateEventCommandHandler extends AbstractEventCommandHandler {
12
  constructor(
13
    @Inject('ISchoolRepository') schoolRepository: ISchoolRepository,
14
    @Inject('IUserRepository') userRepository: IUserRepository,
15
    @Inject('IEventRepository') 
16
    private readonly eventRepository: IEventRepository,
17
  ) {
18
    super(schoolRepository, userRepository);
19
  }
20
21
  public async execute(command: UpdateEventCommand): Promise<string> {
22
    const { id, date, summary, userId, schoolId } = command;
23
24
    const event = await this.eventRepository.findOneById(id);
25
    if (!event) {
26
      throw new EventNotFoundException();
27
    }
28
29
    const [ user, school ] = await Promise.all([
30
      this.getUser(userId),
31
      this.getSchool(schoolId)
32
    ]);
33
34
    event.update(
35
      date,
36
      user,
37
      school,
38
      summary
39
    );
40
41
    await this.eventRepository.save(event);
42
43
    return event.getId();
44
  }
45
}
46